capture log close inequal
log using abs-rel-q1-inequal.txt, name(inequal) replace text

//  program:    abs-rel-q1-inequal.do
//  task:		analysis of absolute and relative inequality judgments
// 	input:		exp1.dta						
//	output:		none
//  project:    MEDEC experiment 1
//  author:     sam harper \ 25mar2013

//  #0
//  program setup

version 12
set linesize 80
clear all
macro drop _all


// #1
// bring in experiment 1 data

use exp1.dta, clear
datasignature confirm

* dummy variables for inequality judgment and graph treatment
dummieslab q1inequal
dummieslab treatment

// #2
// overall frequencies of response to q1, by treatment
gen q1inequalg1=q1inequal if scennum==2 | scennum==6
gen q1inequalg2=q1inequal if scennum==7 | scennum==8
gen q1inequalg3=q1inequal if scennum==3 | scennum==4
gen q1inequalg4=q1inequal if scennum==1 | scennum==5
gen q1inequalg5=q1inequal if scennum==9 | scennum==10

label values q1inequalg* q1

forvalues i=1/5{
	disp "Time 1"
	tab treatment q1inequalg`i' if time==0, row chi2 expect exact
	disp "Time 2"
	tab treatment q1inequalg`i' if time==1, row chi2 expect exact
	}

	
// #3
// Scenario: decreasing abs, increasing relative

* first test for EMM by scenario status
logit increasing i.time##i.treatment##i.mag if scennum==2 | scennum==6, ///
	cluster(subject) cformat(%4.3f) nolog vsquish
	
* marginal effects to test for EMM on absolute scale
margins, dydx(treatment) over(mag time) cformat(%4.3f) post
lincom _b[1.treatment:0.mag#1.time] - _b[1.treatment:0.mag#0.time]
lincom _b[1.treatment:1.mag#1.time] - _b[1.treatment:1.mag#0.time]
lincom (_b[1.treatment:0.mag#1.time] - _b[1.treatment:0.mag#0.time]) ///
 	   - (_b[1.treatment:1.mag#1.time] - _b[1.treatment:1.mag#0.time])

* p-value for timeXtreatmentXmag = 0.236 so drop product term 
* and adjust for scenario
logit increasing i.time##i.treatment i.mag if scennum==2 | scennum==6, ///
	cluster(subject) cformat(%4.3f) nolog vsquish
estimates store q1s26

* predicted probabilities
margins time#treatment mag, cformat(%4.3f)
qui margins time#treatment 
marginsplot, legend(title("Experimental condition:", size(medsmall))) ///
	title("Predicted probability of judging inequality as having increased", ///
	size(medsmall)) scheme(sj) xsc(r(-.2 1.2)) xtitle(Time) name(q1s26, replace) ///
	ytitle("Probability of agreement with ratio measure", size(medsmall)) ///
	note("Scenarios 2 and 6: Decreasing absolute, increasing relative inequality", size(small))
	
* marginal effects	
margins, over(time) dydx(treatment mag) cformat(%4.3f) post
lincom _b[1.treatment:1.time] - _b[1.treatment:0.time]



// #4
// Scenario: constant abs, increasing relative

* first test for EMM by scenario status
logit increasing i.time##i.treatment##i.mag if scennum==7 | scennum==8, ///
	cluster(subject) cformat(%4.3f) nolog vsquish

* marginal effects to test for EMM on absolute scale
margins, dydx(treatment) over(mag time) cformat(%4.3f) post
lincom _b[1.treatment:0.mag#1.time] - _b[1.treatment:0.mag#0.time]
lincom _b[1.treatment:1.mag#1.time] - _b[1.treatment:1.mag#0.time]
lincom (_b[1.treatment:0.mag#1.time] - _b[1.treatment:0.mag#0.time]) ///
 	   - (_b[1.treatment:1.mag#1.time] - _b[1.treatment:1.mag#0.time])
 	   
* p-value for timeXtreatmentXmag = 0.152 so drop product term 
* and adjust for scenario
logit increasing i.time##i.treatment i.mag if scennum==7 | scennum==8, ///
	cluster(subject) cformat(%4.3f) nolog vsquish
estimates store q1s78

* predicted probabilities
margins time#treatment mag, cformat(%4.3f)
qui margins time#treatment 
marginsplot, legend(title("Experimental condition:", size(medsmall))) ///
	title("Predicted probability of judging inequality as having increased", ///
	size(medsmall)) scheme(sj) xsc(r(-.2 1.2)) xtitle(Time) name(q1s78, replace) ///
	ytitle("Probability of agreement with ratio measure", size(medsmall)) ///
	note("Scenarios 7 and 8: Constant absolute, increasing relative inequality", size(small))
	
* marginal effects	
margins, over(time) dydx(treatment mag) cformat(%4.3f) post
lincom _b[1.treatment:1.time] - _b[1.treatment:0.time]



// #5
// Scenario: decreasing abs, constant relative

* first test for EMM by scenario status
logit same i.time##i.treatment##i.mag if scennum==3 | scennum==4, ///
	cluster(subject) cformat(%4.3f) nolog vsquish

* marginal effects to test for EMM on absolute scale
margins, dydx(treatment) over(mag time) cformat(%4.3f) post
lincom _b[1.treatment:0.mag#1.time] - _b[1.treatment:0.mag#0.time]
lincom _b[1.treatment:1.mag#1.time] - _b[1.treatment:1.mag#0.time]
lincom (_b[1.treatment:0.mag#1.time] - _b[1.treatment:0.mag#0.time]) ///
 	   - (_b[1.treatment:1.mag#1.time] - _b[1.treatment:1.mag#0.time])

* p-value for timeXtreatmentXmag = 0.4 so drop product term 
* and adjust for scenario	
logit same i.time##i.treatment i.mag if scennum==3 | scennum==4, ///
	cluster(subject) cformat(%4.3f) nolog vsquish
estimates store q1s34

* predicted probabilities
margins time#treatment mag, cformat(%4.3f)
qui margins time#treatment 
marginsplot, legend(title("Experimental condition:", size(medsmall))) ///
	title("Predicted probability of judging inequality as having decreased", ///
	size(medsmall)) scheme(sj) xsc(r(-.2 1.2)) xtitle(Time) name(q1s34, replace) ///
	ytitle("Probability of agreement with ratio measure", size(medsmall)) ///
	note("Scenarios 3 and 4: Decreasing absolute, constant relative inequality", size(small))

* marginal effects	
margins, over(time) dydx(treatment mag) cformat(%4.3f) post
lincom _b[1.treatment:1.time] - _b[1.treatment:0.time]



// #6
// Scenario: decreasing abs, decreasing relative

* first test for EMM by scenario status
logit decreasing i.time##i.treatment##i.mag if scennum==1 | scennum==5, ///
	cluster(subject) cformat(%4.3f) nolog vsquish
	
* marginal effects to test for EMM on absolute scale
margins, dydx(treatment) over(mag time) cformat(%4.3f) post
lincom _b[1.treatment:0.mag#1.time] - _b[1.treatment:0.mag#0.time]
lincom _b[1.treatment:1.mag#1.time] - _b[1.treatment:1.mag#0.time]
lincom (_b[1.treatment:0.mag#1.time] - _b[1.treatment:0.mag#0.time]) ///
 	   - (_b[1.treatment:1.mag#1.time] - _b[1.treatment:1.mag#0.time])

* p-value for timeXtreatmentXmag = 0.573 so drop product term 
* and adjust for scenario
logit decreasing i.time##i.treatment i.mag if scennum==1 | scennum==5, ///
	cluster(subject) cformat(%4.3f) nolog vsquish
estimates store q1s15

* predicted probabilities
margins time#treatment mag, cformat(%4.3f)
qui margins time#treatment 
marginsplot, legend(title("Experimental condition:", size(medsmall))) ///
	title("Predicted probability of judging inequality as having decreased", ///
	size(medsmall)) scheme(sj) xsc(r(-.2 1.2)) xtitle(Time) name(q1s15, replace) ///
	ytitle("Probability of agreement with ratio measure", size(medsmall)) ///
	note("Scenarios 1 and 5: Decreasing absolute, decreasing relative inequality", size(small))
	
* marginal effects	
margins, over(time) dydx(treatment mag) cformat(%4.3f) post
lincom _b[1.treatment:1.time] - _b[1.treatment:0.time]



// #7
// Scenario: increasing abs, increasing relative

* first test for EMM by scenario status
logit increasing i.time##i.treatment##i.mag if scennum==9 | scennum==10, ///
	cluster(subject) cformat(%4.3f) nolog vsquish

* marginal effects to test for EMM on absolute scale
margins, dydx(treatment) over(mag time) cformat(%4.3f) post
lincom _b[1.treatment:0.mag#1.time] - _b[1.treatment:0.mag#0.time]
lincom _b[1.treatment:1.mag#1.time] - _b[1.treatment:1.mag#0.time]
lincom (_b[1.treatment:0.mag#1.time] - _b[1.treatment:0.mag#0.time]) ///
 	   - (_b[1.treatment:1.mag#1.time] - _b[1.treatment:1.mag#0.time])

* p-value for timeXtreatmentXmag = 0.471 so drop product term 
* and adjust for scenario
logit increasing i.time##i.treatment i.mag if scennum==9 | scennum==10, ///
	cluster(subject) cformat(%4.3f) nolog vsquish
estimates store q1s910

* predicted probabilities
margins time#treatment mag, cformat(%4.3f)
qui margins time#treatment 
marginsplot, legend(title("Experimental condition:", size(medsmall))) ///
	title("Predicted probability of judging inequality as having increased", ///
	size(medsmall)) scheme(sj) xsc(r(-.2 1.2)) xtitle(Time) name(q1s910, replace) ///
	ytitle("Probability of agreement with ratio measure", size(medsmall)) ///
	note("Scenarios 9 and 10: Increasing absolute, increasing relative inequality", size(small))

* marginal effects	
margins, over(time) dydx(treatment mag) cformat(%4.3f) post
lincom _b[1.treatment:1.time] - _b[1.treatment:0.time]



// #8
// table of all effect estimates
esttab q1s26 q1s78 q1s34 q1s15 q1s910 using q1est.rtf, ///
	b(%5.3f) ci(%4.2f) ///
	keep(1.time 1.treatment 1.time#1.treatment 1.mag _cons) ///
	collabels(Coef. 95\%CI) nostar not replace compress ///
	title("Inequality judgment")




// #9
// graphs for logistic regression results

* collapse scenarios
recode scennum (2 6 = 1 "Difference decreases, Ratio increases") ///
	(7 8 =2 "Difference constant, Ratio increases") ///
	(3 4 = 3 "Difference decreases, Ratio constant") ///
	(1 5 = 4 "Difference decreases, Ratio decreases") ///
	(9 10 = 5 "Difference increases, Ratio increases"), gen(grscen)
	
* recode outcome to be constant for all analyses
gen outcome=increasing if inlist(grscen,1,2,5)
replace outcome=same if grscen==3
replace outcome=decreasing if grscen==4

* use marginal estimates to make a single graph
qui logit outcome i.grscen##(i.time##i.treatment) i.mag if grscen<4, ///
	cluster(subject) cformat(%4.3f) nolog vsquish
qui margins time#treatment, over(grscen)
marginsplot, xsc(r(-.2 1.2)) xtitle("") by(grscen) ///
	byopts(rows(1) title("Scenarios with inconsistent absolute and relative inequality trends", size(medsmall)) ///
	legend(off) graphregion(lcolor(white) fcolor(white))) ///
	ylab(0(.2)1)  ///
	subtitle(, size(vsmall) fcolor(white) lcolor(white)) ///
	ytitle("Probability of agreement" "with ratio measure", size(vsmall)) name(con, replace) ///
	fysize(42)
	
qui logit outcome i.grscen##(i.time##i.treatment) i.mag if grscen>3 ///
	& grscen<11, cluster(subject) cformat(%4.3f) nolog vsquish
qui margins time#treatment, over(grscen)
marginsplot, xsc(r(-.2 1.2)) xtitle(Time) by(grscen) ///
	byopts(rows(1) title("Scenarios with consistent absolute and relative inequality trends", size(medsmall)) ///
	graphregion(lcolor(white) fcolor(white) margin(l=20 r=20))) ///
	legend(order(3 "shown raw data and difference measure of inequality" 4 "shown raw data and ratio measure of inequality") ///
	cols(1) rowgap(0.5) size(small) symxsize(8) ///
	title("Experimental condition:", size(small)) width(95)) ///
	subtitle(, size(vsmall) fcolor(white) lcolor(white)) ///
	ytitle("Probability of agreement" "with ratio measure", size(vsmall)) name(incon, replace) ///
	ylab(0(.2)1)
	
graph combine con incon, rows(2) xsize(3.5) graphregion(lcolor(white) fcolor(white))

* predicted margins for exporting to R graphs	
qui regress q2success i.grscen##(i.time##i.treatment) i.mag if grscen<11, ///
	cluster(subject)
margins time#treatment, over(grscen)


log close inequal
exit
